//
//  Dic+.swift
//  JavaShop
//
//  Created by LDD on 2018/8/29.
//  Copyright © 2018年 LDD. All rights reserved.
//

import Foundation

// MARK: - 字典扩展方法
extension NSDictionary {
    
    /// 获取字符串数据
    ///
    /// - Parameters:
    ///   - key: 数据索引
    ///   - defaultValue: 默认值
    /// - Returns: 字符串数据
    func stringValue(key :String , defaultValue : String = "") -> String{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! String
        }
    }
    
    func timeStringValue(key :String,format :String = "yyyy-MM-dd HH:mm:ss") ->String{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return ""
        }else{
            let timestamp = data as! Int
            let date = Date.init(timeIntervalSince1970: TimeInterval(timestamp))
            let dformatter = DateFormatter()
            dformatter.dateFormat = format
            let dateStr = dformatter.string(from: date)
            return dateStr
        }
    }
    
    /// 获取字符串数据
    ///
    /// - Parameters:
    ///   - key: 数据索引
    ///   - defaultValue: 默认值
    /// - Returns: 字符串数据
    func boolValue(key :String , defaultValue : Bool = false) -> Bool{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! Bool
        }
    }
    
    /// 获取int数据
    ///
    /// - Parameters:
    ///   - key: 数据索引
    ///   - defaultValue: 默认数据
    /// - Returns: int数据
    func intValue(key :String , defaultValue : Int = 0) -> Int{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! Int
        }
    }
    
    /// 双精度数据
    ///
    /// - Parameters:
    ///   - key: 数据索引
    ///   - defaultValue: 默认数据
    /// - Returns: 双精度数据
    func doubleValue(key :String , defaultValue : Double = 0.0) -> Double{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! Double
        }
    }
    
    /// 单精度数据
    ///
    /// - Parameters:
    ///   - key: 索引
    ///   - defaultValue: 默认数据
    /// - Returns: 单精度数据
    func cgFloatValue(key :String , defaultValue : Float = 0.0) -> Float{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! Float
        }
    }
    
    /// 数组数据
    ///
    /// - Parameters:
    ///   - key: 索引
    ///   - defaultValue: 默认值
    /// - Returns: 数据数据
    func arrayValue(key :String , defaultValue : NSArray = []) -> NSArray{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! NSArray
        }
    }
    
    /// 字典数据
    ///
    /// - Parameters:
    ///   - key: 索引
    ///   - defaultValue: 默认数据
    /// - Returns: 字典数据
    func dicValue(key :String , defaultValue : NSDictionary = [:]) -> NSDictionary{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! NSDictionary
        }
    }
    
    /// 对象数据
    ///
    /// - Parameters:
    ///   - key: 数据索引
    ///   - defaultValue: 默认数据
    /// - Returns: 对象数据
    func objectValue<T>(key :String , defaultValue : T) -> T{
        let data : Any? = value(forKey: key)
        if (data is NSNull || data == nil){
            return defaultValue
        }else{
            return data as! T
        }
    }
    
    
}



extension NSArray{
    
    func mapDic() -> [NSDictionary]{
        return map({ item -> NSDictionary in
            return item as! NSDictionary
        })
    }
    
}
